// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: SSZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 0 : CUTSCENE_NONE
private alias 1 : CUTSCENE_MIDZONE
private alias 2 : CUTSCENE_ENDZONE
private alias 666 : GROUP_CUTSCENES

// Function declarations
reserve function SSZSetup_SpeedUpMusic1P
reserve function SSZSetup_SlowDownMusic1P
reserve function SSZSetup_SpeedUpMusic2P
reserve function SSZSetup_SlowDownMusic2P
reserve function SSZSetup_LoadPlayerStartPos

// Static Values
public value SSZSetup_value33 = 0;
public value LightningTimer = -50;
public value LightningWarningTimer = -175;

private alias SSZSetup_value33 : SSZSetup_AchievementValue

// Tables
public table SSZSetup_table39
	508, 24, 509, 10, 510, 12, 511, 24, 510, 12, 509, 10
end table

public table SSZSetup_table40
	512, 128, 513, 20, 512, 8, 513, 8, 512, 8, 513, 8
end table

public table SSZSetup_table41
	514, 128, 515, 12, 514, 12, 515, 12, 514, 6, 515, 6, 514, 6, 515, 6
end table

public table SSZSetup_table42
	516, 8, 517, 8
end table

public table SSZSetup_table43
	518, 128, 519, 8, 518, 8, 519, 8, 518, 8, 519, 12, 518, 12, 519, 12
	
end table

public table SSZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

private table SSZSetup_demoTable1
	0x440000, 0x2910000, 32, 1, 40, 27, 8, 39, 56, 1, 40, 9, 8, 40, 56, 1, 40, 6, 8, 8, 56, 1, 40, 6, 8, 175, 56, 1, 40, 7, 8, 111, 0, 2, 4, 35, 52, 1, 36, 7, 4, 73, 0, 4, 8, 4, 56, 1, 40, 7, 8, 68, 0, 5, 4, 30, 0, 8, 2, 4, 50, 1, 34, 1, 50, 1, 32, 4, 0, 15, 8, 56, 0, 1, 2, 32, 50, 1, 34, 1, 42, 1, 40, 3, 8, 255, 8, 97, 0, 12, 4, 5, 0, 26, 8, 21, 56, 1, 40, 9, 8, 255, 8, 4, 10, 3, 2, 7, 10, 2
end table

private table SSZSetup_demoTable2
	0x440000, 0x2950000, 32, 13, 40, 38, 8, 47, 56, 1, 40, 12, 8, 224, 56, 1, 40, 5, 8, 69, 56, 1, 40, 8, 32, 2, 0, 2, 8, 12, 0, 28, 8, 16, 0, 1, 8, 255, 8, 5, 56, 1, 40, 24, 8, 218, 56, 1, 40, 9, 8, 50, 0, 1, 4, 7, 0, 16, 8, 174, 56, 1, 40, 12, 8, 7, 56, 1, 40, 8, 8, 4, 56, 1, 40, 5, 8, 3, 4, 2, 52, 1, 36, 5, 4, 4, 52, 1, 32, 5, 0, 4, 4, 1, 52, 1, 36, 3, 4, 4, 52, 1, 36, 4, 4, 5, 52, 1, 36, 3
	4, 1, 0, 2, 8, 2, 56, 1, 40, 3, 32, 2, 0, 1, 8, 5, 0, 5, 8, 3, 56, 1, 40, 2, 8, 1, 0, 9, 8, 5, 0, 15, 8, 22, 0, 3, 4, 2, 52, 1, 36, 9, 32, 2, 40, 8, 8, 4, 56, 1, 40, 4, 32, 1, 0, 3, 48, 1, 32, 6, 0, 4, 48, 1, 32, 5, 0, 1, 8, 3, 0, 1, 48, 1, 32, 5, 0, 3, 48, 1, 32, 3, 40, 3, 8, 2, 0, 1, 48, 1, 32, 5, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 4, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 4, 0, 5, 48, 1, 32, 5, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 3, 48, 1, 32, 5, 0, 3, 48, 1, 32, 6, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 40, 2, 8, 39, 56, 1, 40, 12, 8, 13, 0, 17, 8, 4, 0, 12, 4, 11, 8, 8
end table

private table SSZSetup_demoTable3
	0x440000, 0x2910000, 0, 18, 8, 77, 56, 1, 40, 13, 8, 70, 56, 1, 40, 14, 8, 3, 0, 22, 4, 4, 0, 3, 48, 1, 32, 16, 40, 3, 8, 1, 0, 30, 48, 1, 32, 38, 40, 149, 8, 11, 0, 3, 4, 6, 0, 4, 4, 5, 0, 5, 8, 13, 0, 14, 4, 89, 0, 6, 8, 44, 10, 1, 2, 13, 0, 52, 8, 59, 56, 1, 40, 17, 8, 18, 0, 4, 4, 33, 0, 1, 8, 105, 56, 1, 40, 30, 8, 45, 4, 6, 0, 52, 8, 6, 56, 1, 40, 19, 8, 54, 10, 6, 2, 17, 10, 2, 8, 58, 56, 1, 40, 4, 8, 197, 56, 1, 41, 10, 9, 14, 57, 1, 41, 40, 33, 4, 1, 126, 9, 12, 8, 6, 56, 1, 40, 12, 
end table

function SSZSetup_LoadPlayerStartPos
		if cutscene.mode == CUTSCENE_MIDZONE
				temp0 = 256
				temp0 += player.saveX
				object[0].ixpos = temp0

				object[0].iypos = player.saveY

				object[0].value37 = items.save
				CallFunction(PlayerObject_RestorePowerup)
				temp0 = 256
				temp0 -= screen.xcenter
				stage.curXBoundary1 = temp0
				
				temp0 += screen.xsize
				stage.curXBoundary2 = temp0
				
				//stage.curYBoundary1 = camera.saveY1
				//stage.curYBoundary2 = camera.saveY2
				stage.minutes = 0
				stage.seconds = 0
				stage.milliSeconds = 0
		endif
end function

function SSZSetup_SpeedUpMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 1)
			SwapMusicTrack("SPEEDSHOES.ogg", 0, 0, 1)
			break
		case 2
			SetMusicTrack("SPEEDSHOES.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("SPEEDSHOES.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function SSZSetup_SlowDownMusic1P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 1)
			SwapMusicTrack("SSPLASH.ogg", 0, 1, 1)
			break
		case 2
			SetMusicTrack("SSPLASH.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("SSPLASH.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 1)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function

function SSZSetup_SlowDownMusic2P
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 1)
			SwapMusicTrack("SSPLASH2.ogg", 0, 1, 1)
			break
		case 2
			SetMusicTrack("SSPLASH2.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0, 1)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("SSPLASH2.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 1)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function

event ObjectMain
	if LightningWarningTimer >= 40
		LightningTimer++
	end if
	LightningWarningTimer++
		object.value10++
		if object.value10 > 7
			tileLayer[2].deformationOffset++
			object.value10 = 0
		end if
	if object.state < 5
		object.value1 ++
		if object.value1 == 30
			object.value1 = 0
			//RotatePalette(0, 147, 150, 0)
		end if
	end if
end event


event ObjectStartup
	switch stage.actNum
	case 1
		rankS = 005099
		rankA = 006099
		rankB = 007599
		rankC = 009099
		SetMusicTrack("SSPLASH.ogg", 0, 1)
		LoadPalette("SSZ/Act1.act", 0, 0, 0, 255)
		SlowDownMusic = SSZSetup_SlowDownMusic1P
		break
	case 2
		rankS = 005099
		rankA = 006599
		rankB = 008899
		rankC = 009099
		SetMusicTrack("SSPLASH2.ogg", 0, 1)
		if options.expertmode == 1
			LoadPalette("SSZ/Act2Expert.act", 0, 0, 0, 255)
		else
			LoadPalette("SSZ/Act2.act", 0, 0, 0, 255)
		end if
		SlowDownMusic = SSZSetup_SlowDownMusic2P
		callFunction(SSZSetup_LoadPlayerStartPos)
		break
	case 3
		rankS = 000000
		rankA = 000001
		rankB = 000002
		rankC = 000003
		StopMusic()
		if options.expertmode == 1
			LoadPalette("SSZ/Act2Expert.act", 0, 0, 0, 255)
		else
			LoadPalette("SSZ/Act2.act", 0, 0, 0, 255)
		end if
		boss.health = 0
		boss.health.max = 230
		break
	end switch

	LoadPalette("SSZ/BW.act", 2, 0, 0, 255)
	SpeedUpMusic = SSZSetup_SpeedUpMusic1P
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Ricky]
	//tileLayer[1].scrollPos = -0x80000

	arrayPos0 = 256
	while arrayPos0 < 576
		arrayPos1 = arrayPos0
		arrayPos1 -= 256
		Rand(stage[arrayPos1].deformationData2, 4)
		temp0 = arrayPos0
		temp0 &= 1
		if temp0 == 1
			FlipSign(stage[arrayPos1].deformationData2)
		end if
		stage[arrayPos0].deformationData2 = stage[arrayPos1].deformationData2
		arrayPos0++
	loop

	Stage[0].ActiveLayer= 1
	Stage[1].ActiveLayer= 2
	object[10].type = TypeName[SSZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[20].type = TypeName[BG Clouds]
	object[21].priority = PRIORITY_ACTIVE
	object[21].Type = 40 //Pause Menu, TypeName does not work since it's in gameconfig
	object[21].priority = PRIORITY_ACTIVE_PAUSED
	SetPaletteEntry(0, 192, 0)
	if options.attractMode == true
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = SSZSetup_demoTable1
			PlayerObject_ReplaySize = 110
			PlayerObject_ReplayLength = 0x654
			break
		case 1
			PlayerObject_ReplayTable = SSZSetup_demoTable2
			PlayerObject_ReplaySize = 318
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = SSZSetup_demoTable3
			PlayerObject_ReplaySize = 138
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
